package fumeurPolie;
import java.util.concurrent.Semaphore;


public class TableSingleton {
	private static TableSingleton table = null;
	private Semaphore mutex = new Semaphore(1, true);
	public Semaphore tableVide = new Semaphore(1,true);
	private boolean ressource[] = new boolean[3];

	private TableSingleton(){
		ressource[0] = false;
		ressource[1] = false;
		ressource[2] = false;
	}
	public static synchronized TableSingleton GetInstance(){
		if(table == null){
			table = new TableSingleton();
		}
		return table;
	}
	
	public boolean VerifierIngredientDisponible(boolean[] ressourceAcquise) throws InterruptedException{
		boolean disponible = false;
		mutex.acquire();
		boolean possedeDeja = false;
		for(int i = 0; i < 3 && !possedeDeja; i++){
			possedeDeja = (ressource[i] == ressourceAcquise[i]);
		}
		
		disponible = !possedeDeja;
		if(!disponible){
			mutex.release();
		}
		return disponible;
	}
	
	public void UtiliserIngredient(){
		for(int i = 0; i < 3; i++){
			ressource[i] = false;
		}
		tableVide.release();
		mutex.release();
		
	}
	
	public void AjouterIngredient(int ingredient1, int ingredient2) throws InterruptedException{
		mutex.acquire();
		ressource[ingredient1] = true;
		ressource[ingredient2] = true;
		System.out.println("Les ingrédients " + ingredient1 + " et " + ingredient2 + " sont déposés");
		mutex.release();
	}
}
